package pers.li.controller.login;

import pers.li.model.User;
import pers.li.service.UserService;
import pers.li.utils.JsonUtil;
import pers.li.utils.PublicUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@WebServlet("/reg.action")
public class RegController extends HttpServlet {
    private final UserService userService = new UserService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = PublicUtils.getObjectByRequest(User.class, req);
        assert user != null;
        Map<String, Object> regUser = userService.regUser(user);
        req.getSession().setAttribute("login",regUser);
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter printWriter = resp.getWriter();
        printWriter.print(JsonUtil.toJsonString(regUser));
        printWriter.flush();
        printWriter.close();
    }
}
